﻿using Domain.Types;
using Infrastructure.AggregateBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Domain.Model
{
    [Table("Task")]
    public class TaskAggregate : AggregateRoot<TaskAggregate, int>
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public override int ID { get; set; }
        [StringLength(200)]
        public string Name { get; set; }

        public int CategoryID { get; set; }
        /// <summary>
        /// 过期时间,任务完成
        /// </summary>
        public DateTime? FinishTime { get; set; }
        /// <summary>
        /// 所有数量完成，任务完成
        /// </summary>
        public bool IsFinishAll { get; set; }
        /// <summary>
        /// 限时提交{多少时间后自动取消}
        /// </summary>
        public LimitTimeEnum LimitTime { get; set; }
        /// <summary>
        /// 审核周期{多少时间后自动完成}
        /// </summary>
        public LimitAuditEnum LimitAudit { get; set; }
        /// <summary>
        /// 字段数据
        /// </summary>
        [StringLength(2000)]
        public string FieldData { get; set; }
        /// <summary>
        /// 任务步骤数据
        /// </summary>
        [StringLength(2000)]
        public string StepFieldData { get; set; }
        /// <summary>
        /// 提交数据
        /// </summary>
        public string SubFieldName { get; set; }
        /// <summary>
        /// 需要提交数据
        /// </summary>
        public bool NeedSubField { get; set; }
        /// <summary>
        /// 排序时间 {刷新}
        /// </summary>
        public DateTime FreshTime { get; set; } = DateTime.Now;
        public TaskStatus Status { get; set; }
        public int UserID { get; set; }
        /// <summary>
        /// 总次数
        /// </summary>
        public int TotalCount { get; set; }
        /// <summary>
        /// 可用次数
        /// </summary>
        public int RemainCount { get; set; }
        /// <summary>
        /// 完成次数
        /// </summary>
        public int BeFinishCount { get; set; }
        /// <summary>
        /// 单价
        /// </summary>
        public int Price { get; set; }

        //[ConcurrencyCheck]
        //public override DateTime UT { get => base.UT; set => base.UT = value; }
        [Timestamp]
        public byte[] Version { get; set; }

        public virtual List<TaskAudit> TaskAudits { get; set; }

        /// <summary>
        /// 获取字段数据
        /// </summary>
        /// <returns></returns>
        public List<TaskFieldInfo> GetDataField()
        {
            if (string.IsNullOrWhiteSpace(FieldData)) return null;

            return JsonConvert.DeserializeObject<List<TaskFieldInfo>>(FieldData);
        }
        /// <summary>
        /// 设置字段数据
        /// </summary>
        /// <param name="taskFieldInfos"></param>
        public void SetDataField(List<TaskFieldInfo> taskFieldInfos)
        {
            FieldData = JsonConvert.SerializeObject(taskFieldInfos);
        }


        /// <summary>
        /// 获取步骤数据
        /// </summary>
        /// <returns></returns>
        public List<TaskStepFieldInfo> GetStepDataField()
        {
            if (string.IsNullOrWhiteSpace(StepFieldData)) return null;

            return JsonConvert.DeserializeObject<List<TaskStepFieldInfo>>(StepFieldData);
        }
        /// <summary>
        /// 设置步骤数据
        /// </summary>
        /// <param name="taskFieldInfos"></param>
        public void SetStepDataField(List<TaskStepFieldInfo> taskStepFieldInfos)
        {
            StepFieldData = JsonConvert.SerializeObject(taskStepFieldInfos);
        }

    }
}
